<?php
/*
 * 手机客户端用户-登录接口
 * 2014-06-03
 */
 
 //加价幅度
function checkRegKey($mobile,$imsi,$sourceacc,$key,$clientplat){
	if (isset($mobile) and isset($IMSI) and isset($sourceacc) and isset($key)){
		//验证字段(手机号+IMSI+平台+来源+日期天 的MD5)
		$vkey = md5($mobile.$imsi.$clientplat.$sourceacc);
		if ($key==$vkey){
			return true;
		}else{
			return false;
		}
	}else{
		return false;
	}
}

include "../inc/public_header.php";
include "../func/user.php";				//用户信息处理函数

//常量
$mobile 	= $_REQUEST["mobile"];		
$userpwd 	= $_REQUEST["userpwd"];	
$key 		= $_REQUEST["key"];	
$imsi 		= $_REQUEST["imsi"];	
$clientplat = $_REQUEST["clientplat"];	
$sourceacc 	= $_REQUEST["sourceacc"];	

//登录
$success = 0;
$rs=array("success"=>"0");
$m_auth = 0;
if (isset($mobile) and isset($userpwd)){
	if (isset($key)){
		$checkRegKey = checkRegKey($mobile,$imsi,$sourceacc,$key,$clientplat);
		if ($checkRegKey == true){
			//密码是否正确
			$sql = "SELECT uid,uuid,userpwd,muuid FROM user_base WHERE mobile = '".$mobile."'";
			$db->query($sql);
			if($db->next_record()){
				$checkuspwd_uid = $db->f("uid");
				$checkuspwd_uuid = $db->f("uuid");
				$checkuspwd_userpwd = $db->f("userpwd");
				$checkuspwd_muuid = $db->f("muuid");
			}
			if (!$checkuspwd_uid){
				//"msg":"手机号未注册"
				$rs=array("success"=>"0","errCode"=>"3");
			}else{
				if ($checkuspwd_userpwd == $userpwd){
					$sql = "SELECT UUID() as uuid";
					$db->query($sql);
					if($db->next_record()){
						$newmuuid = $db->f("uuid");
					}
					$sql = "update user_base set muuid = '".$newmuuid."' where uid = '".$checkuspwd_uid."'";
					$db->query($sql);
					//插入登入日志
					$sql = "insert into user_login (uid,login";
					if (isset($clientplat) and strlen($clientplat)>0){
						$sql =$sql.",plat";
					}
					if (isset($imsi) and strlen($imsi)>0){
						$sql =$sql.",imsi";
					}
					$sql =$sql.",loginDT) values('".$checkuspwd_uid."','".$mobile."'";
					if (isset($clientplat) and strlen($clientplat)>0){
						$sql =$sql.",'".$clientplat."'";
					}
					if (isset($imsi) and strlen($imsi)>0){
						$sql =$sql.",'".$imsi."'";
					}
					$sql =$sql.",now())";
					$db->query($sql);
					$sql = "SELECT imsi from user_imsi where uid='".$checkuspwd_uid."'";
					$db->query($sql);
					if($db->next_record()){
						if ($db->f("imsi")!=$imsi){
							$sql1 = "update user_imsi set imsi='".$imsi."' where uid='".$checkuspwd_uid."'";
							$db1->query($sql1);
						}
					}else{
						$sql1 = "insert into user_imsi (uid,imsi) values ('".$checkuspwd_uid."','".$imsi."')";
						$db1->query($sql1);
					}
					$rs=array("success"=>"1","uuid"=>$newmuuid);
				}
			}
		}else{
			//"msg":"KEY不正确"
			$rs=array("success"=>"0","errCode"=>"2");
		}
	}else{
		//"msg":"缺少KEY"
		$rs=array("success"=>"0","errCode"=>"-1");
	}
}



echo json_encode($rs);
?>
